// pages/user/userinfo.js
import {getRemoteUserInfo, editRemoteUserInfo} from '../../api/index'
import CAR_NUM_LIST from '../../utils/carAddr.js'
import {baseUrl} from '../../api/index'
import {vModel} from '../../utils/v-model'
import {shareApp} from '../../utils/shareApp'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: baseUrl,
    userInfo: {},
    areaOptions: CAR_NUM_LIST,
    areaIndex: 0,
    form: {
      name: '',
      phone: '',
      id_number: '',
      car_number: ''
    },
    selectAreaPopupVisible: false
  },
  // bindInput
  bindInput (e) {
    vModel(this, e)
  },
  // 跳转到修改资料
  bindNavTo (e) {
    let {handle} = e.currentTarget.dataset
    let {phone, group_id} = this.data.userInfo
    wx.navigateTo({
      url: `/pages/user/validate?from=userinfo&handle=${handle}&phone=${phone}&role=${group_id}`,
    })
  },
  // 图片预览
  bindPreviewImage(e) {
    if (this.data.userInfo.is_verify == 0) return
    const current = e.target.dataset.src
    console.log(current)
    wx.previewImage({
      current,
      urls: [current]
    })
  },
  
  bindEditUserInfo () {
    let _data = this.data
    let _form = _data.form
    let _car_number = _form.car_number.trim()
    if (!_car_number) {
      wx.showToast({
        icon: 'none',
        title: '请输入车牌号'
      })
      return
    }
    // _data.areaOptions[_data.areaIndex] +
    editRemoteUserInfo({car_number: _form.car_number}).then(res => {
      wx.showToast({
        icon: 'none',
        title: res.data.info
      })
    }).catch(err => {
      wx.showToast({
        icon: 'none',
        title: err.data.info
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let _areaOptions = _this.data.areaOptions
    getRemoteUserInfo().then(res => {
      let _info = res.data.info
      // let _carNumber = _info.car_number
      // let _areaIndex = _areaOptions.findIndex(item => item === _abbrName)
      // let _userInfo = Object.assign({}, _info, {car_number: _carNumber, car_abbr: _abbrName})
      this.setData({
        userInfo: _info,
        'form.car_number': _info.car_number
        // areaIndex: _areaIndex === -1 ? 0 : _areaIndex
      })
      console.log(_info)
    }).catch(err => {
      console.log(err)
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return shareApp()
  }
})
